/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package poproject.GUI;


import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.zip.DataFormatException;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import poproject.CombinatorialTask.CombinatorialTaskEngine;
import poproject.Common.JaCoPEngine;
import poproject.Common.DataConverter;
import poproject.CombinatorialTask.Word;
import poproject.GUI.select.ValueSelectMax;
import poproject.GUI.select.ValueSelectMin;
import poproject.OptimizationTask.MapModel;
import poproject.OptimizationTask.MapTrack;
import poproject.OptimizationTask.OptimizationTaskEngine;
import poproject.PlanningTask.FootballTeam;
import poproject.PlanningTask.PlanningTaskEngine;


/**
 *
 * @author Jan Karaś, Mateusz Maciąg
 */
public class MainWindow extends javax.swing.JFrame {


    private ArrayList<Number> mTimeArray;   //< Zmienna reprezentująca dane z panelu ram czasowych
    private JTable mTeamsTable;             //< Zmienna reprezentująca dane z panelu drużyn
    private PTConfigWindow mConfigWindow;   //< Zmienna reprezentująca okno edycyjne
    private FootballTeamPanel mTeamPanel;   //< Zmienna reprezentująca panel drużyn
    private TimePanel mTimePanel;           //< Zmienna reprezentująca panel ram czasowych
    private CostPlanerPanel mCostPlanerPanel; //< Zmienna reprezentująca edytor mapy
    private OptimizationTaskEngine mOptimizationTask;
     
    /**
     * Creates new form MainWindow
     */
    public MainWindow() {
        this.mConfigWindow = new PTConfigWindow();
        this.mTimePanel = new TimePanel(this, mConfigWindow);
        this.mTeamPanel = new FootballTeamPanel(this, mConfigWindow);
        this.mCostPlanerPanel = new CostPlanerPanel(this, mConfigWindow);
        initComponents();
        setDefaultParam();
        setSelectionMethods();
        this.setTitle("Programowanie Ograniczeniowe - JaCoP");    
        
        MapRecalcListener mapListener = new MapRecalcListener(jMapVisualizer, jTrackResult);
    }
    
    /**
     * Wypełnia kontrolki metodami wyboru zmiennych
     */
    private void setSelectionMethods()
    {
        /*jValueSelMethod.removeAllItems();
        jValueSelMethod.addItem(new ValueSelectMax());
        jValueSelMethod.addItem(new ValueSelectMin());
        jValueSelMethod.addItemListener(null);*/
    }
    
    /**
     * Ustawienie domyślnych parametrów i inicializacja zmiennych.
     */
    private void setDefaultParam()
    {   
        mTeamsTable = new JTable();
        mTimeArray = new ArrayList<>();
        mConfigWindow.setResizable(false);
        mConfigWindow.setLayout(new CardLayout());
    }
    
    /**
     * Ustawienie listy przedziałów czasowych.
     * Metoda pozwala na ustawienie listy w oknie głównym która reprezentuje
     * dane czasowe turnieju.
     * @param timeArray Lista zmiennych opisujących ramy czasowe turnieju
     */
    public void setTimeArray(ArrayList<Number> timeArray)
    {
        mTimeArray = timeArray;
    }
    
    /**
     * Pobranie listy przedziałów czasowych.
     * Metoda pozwala na pobranie listy reprezentującej dane czasowe turnieju.
     * @return Lista zmiennych opisujących ramy czasowe turnieju
     */
    public ArrayList<Number> getTimeArray()
    {
        return mTimeArray;
    }
    
    /**
     * Pobranie tabeli drużyn.
     * Metoda pozwala na pobranie tabeli drużyn z okna głównego która 
     * reprezentują dane wprowadzone przez użytkownika w panelu.
     * @return Tabela drużyn
     */
    public JTable getTeamsTable()
    {
        return mTeamsTable;
    }
    
    /**
     * Ustawienie tabeli drużyn.
     * Metoda pozwala nna ustawienie tabeli drużyn w oknie głównym które 
     * reprezentują dane wprowadzone przez użytkownika.
     * @param teamsTable Tabela drużyn
     */
    public void setTeamsTable(JTable teamsTable)
    {
        mTeamsTable = teamsTable;
    }
    
    public void setMapModel(MapModel mapModel)
    { 
        mOptimizationTask = new OptimizationTaskEngine(mapModel);
        mOptimizationTask.createModel();
        mOptimizationTask.searchSolution();
        MapTrack mt = mOptimizationTask.getTrackSolution();
        mapModel.setCurrentTrack(mt);
        
        jMapVisualizer.setMapModel(mapModel);
        jMapVisualizer.repaint();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jTabPanel = new javax.swing.JTabbedPane();
        jPanelOptimizationTask = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jBtnConfig = new javax.swing.JButton();
        jTrackResult = new javax.swing.JLabel();
        jMapVisualizer = new poproject.GUI.MapVisualizer();
        jPanelCombinatorialTask = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextFieldUserInput = new javax.swing.JTextPane();
        jButtonResolve = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextFieldSolution = new javax.swing.JTextPane();
        jButtonClear = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jComboBoxTaskExample = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPanelPlanningTask = new javax.swing.JPanel();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 32767));
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jButtonEnterTeams = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jButtonEnterTimeSpan = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jButtonResolvePT = new javax.swing.JButton();
        jButtonClearPT = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTextAreaUserInf = new javax.swing.JTextArea();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanelRound1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableGameScheduleRound1 = new javax.swing.JTable();
        jPanelRound2 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        jTableGameScheduleRound2 = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItemClose = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanelOptimizationTask.setLayout(new java.awt.BorderLayout());

        jBtnConfig.setText("Konfiguracja");
        jBtnConfig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnConfigActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jBtnConfig, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jTrackResult, javax.swing.GroupLayout.DEFAULT_SIZE, 508, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtnConfig)
                    .addComponent(jTrackResult))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jMapVisualizerLayout = new javax.swing.GroupLayout(jMapVisualizer);
        jMapVisualizer.setLayout(jMapVisualizerLayout);
        jMapVisualizerLayout.setHorizontalGroup(
            jMapVisualizerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jMapVisualizerLayout.setVerticalGroup(
            jMapVisualizerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 317, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jMapVisualizer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jMapVisualizer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanelOptimizationTask.add(jPanel1, java.awt.BorderLayout.PAGE_START);

        jTabPanel.addTab("Optimization Task", jPanelOptimizationTask);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Wprowadź problem do rozwiązania");

        jScrollPane2.setViewportView(jTextFieldUserInput);

        jButtonResolve.setText("Rozwiąż");
        jButtonResolve.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonResolveActionPerformed(evt);
            }
        });

        jTextFieldSolution.setEditable(false);
        jScrollPane3.setViewportView(jTextFieldSolution);

        jButtonClear.setText("Wyczyść");
        jButtonClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonClearActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Rozwiązanie problemu");

        jComboBoxTaskExample.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Przykład 1", "Przykład 2", "Przykład 3", "Przykład 4" }));
        jComboBoxTaskExample.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxTaskExampleActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Opcje");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("Przykładowe problemy");

        javax.swing.GroupLayout jPanelCombinatorialTaskLayout = new javax.swing.GroupLayout(jPanelCombinatorialTask);
        jPanelCombinatorialTask.setLayout(jPanelCombinatorialTaskLayout);
        jPanelCombinatorialTaskLayout.setHorizontalGroup(
            jPanelCombinatorialTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelCombinatorialTaskLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelCombinatorialTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelCombinatorialTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jScrollPane2))
                    .addComponent(jComboBoxTaskExample, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(jPanelCombinatorialTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelCombinatorialTaskLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanelCombinatorialTaskLayout.createSequentialGroup()
                        .addGroup(jPanelCombinatorialTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelCombinatorialTaskLayout.createSequentialGroup()
                                .addComponent(jButtonResolve)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonClear))
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 405, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        jPanelCombinatorialTaskLayout.setVerticalGroup(
            jPanelCombinatorialTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelCombinatorialTaskLayout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(jPanelCombinatorialTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelCombinatorialTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxTaskExample, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonResolve)
                    .addComponent(jButtonClear))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelCombinatorialTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelCombinatorialTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
                    .addComponent(jScrollPane3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabPanel.addTab("Combinatorial Task", jPanelCombinatorialTask);

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel11.setText("Harmonogram rozgrywek");

        jButtonEnterTeams.setText("Wprowadź drużyny");
        jButtonEnterTeams.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEnterTeamsActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Panel konfiguracyjny");

        jButtonEnterTimeSpan.setText("Wprowadź ramy czasowe");
        jButtonEnterTimeSpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEnterTimeSpanActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonEnterTeams, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jButtonEnterTimeSpan, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonEnterTeams)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonEnterTimeSpan)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setText("Opcje");

        jButtonResolvePT.setLabel("Rozwiąż");
        jButtonResolvePT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonResolvePTActionPerformed(evt);
            }
        });

        jButtonClearPT.setText("Wyczyść rozwiązanie");
        jButtonClearPT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonClearPTActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonClearPT, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonResolvePT, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonResolvePT)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonClearPT)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTextAreaUserInf.setEditable(false);
        jTextAreaUserInf.setColumns(20);
        jTextAreaUserInf.setRows(5);
        jScrollPane4.setViewportView(jTextAreaUserInf);

        jTableGameScheduleRound1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Spotkanie", "Boisko", "Czas"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableGameScheduleRound1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTableGameScheduleRound1);

        javax.swing.GroupLayout jPanelRound1Layout = new javax.swing.GroupLayout(jPanelRound1);
        jPanelRound1.setLayout(jPanelRound1Layout);
        jPanelRound1Layout.setHorizontalGroup(
            jPanelRound1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelRound1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanelRound1Layout.setVerticalGroup(
            jPanelRound1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Runda 1", jPanelRound1);

        jTableGameScheduleRound2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Spotkanie", "Boisko", "Czas"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableGameScheduleRound2.getTableHeader().setReorderingAllowed(false);
        jScrollPane5.setViewportView(jTableGameScheduleRound2);

        javax.swing.GroupLayout jPanelRound2Layout = new javax.swing.GroupLayout(jPanelRound2);
        jPanelRound2.setLayout(jPanelRound2Layout);
        jPanelRound2Layout.setHorizontalGroup(
            jPanelRound2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelRound2Layout.createSequentialGroup()
                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanelRound2Layout.setVerticalGroup(
            jPanelRound2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Runda 2", jPanelRound2);

        javax.swing.GroupLayout jPanelPlanningTaskLayout = new javax.swing.GroupLayout(jPanelPlanningTask);
        jPanelPlanningTask.setLayout(jPanelPlanningTaskLayout);
        jPanelPlanningTaskLayout.setHorizontalGroup(
            jPanelPlanningTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPlanningTaskLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelPlanningTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelPlanningTaskLayout.createSequentialGroup()
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(383, 383, 383))
                    .addGroup(jPanelPlanningTaskLayout.createSequentialGroup()
                        .addGroup(jPanelPlanningTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane4)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                        .addGroup(jPanelPlanningTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
        );
        jPanelPlanningTaskLayout.setVerticalGroup(
            jPanelPlanningTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPlanningTaskLayout.createSequentialGroup()
                .addGroup(jPanelPlanningTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanelPlanningTaskLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanelPlanningTaskLayout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jLabel11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTabbedPane1)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanelPlanningTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelPlanningTaskLayout.createSequentialGroup()
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(53, 53, 53))
                    .addComponent(filler1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14))
        );

        jTabPanel.addTab("Planning Task", jPanelPlanningTask);

        jMenu1.setText("File");

        jMenuItemClose.setLabel("Close");
        jMenu1.add(jMenuItemClose);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Help");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 643, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 407, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Wyczyszczenie okien reprezentujących rozwiązanie problemu Kombinatorycznego
     * @param evt 
     */
    private void jButtonClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonClearActionPerformed
        // TODO add your handling code here:
        jTextFieldUserInput.setText(null);
        jTextFieldSolution.setText(null);
    }//GEN-LAST:event_jButtonClearActionPerformed

    /**
     * Rozwiązanie problemu kombinatorycznego.
     * Metoda pozwalająca na rozwiązanie problemu kombinatorycznego
     * @param evt 
     */
    private void jButtonResolveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonResolveActionPerformed
        // TODO add your handling code here:
        String userText = this.jTextFieldUserInput.getText().toUpperCase();
        
        try
        {
            if(Pattern.compile("\\d").matcher(userText).find())
                    throw new DataFormatException("W zagadnieniu występują cyfry! Niedozwolone!");

            ArrayList<Word> wordList = DataConverter.stringToWordArray(userText);

            JaCoPEngine appEngine = new CombinatorialTaskEngine(wordList);
            appEngine.createModel();

            if(appEngine.searchSolution())
            {      
                String solution = DataConverter.getSolutionString(appEngine.getSearchMethod(), wordList);
                this.jTextFieldSolution.setText("Rozwiązanie zostało znalezione!\n\n"+solution);
            }
            else
                this.jTextFieldSolution.setText("Nie udało się znaleźć rozwiązania");
        }
        catch(DataFormatException e)
        {
            JOptionPane.showMessageDialog(rootPane, e.getMessage(), "OSTRZEŻENIE", JOptionPane.WARNING_MESSAGE);
        }
        catch(Exception e)
        {
            String str = "Nie wprowadzono zagadnienia do rozwiązania!\nPamiętaj o wprowadzeniu zagadnienie!";
            JOptionPane.showMessageDialog(rootPane, str, "OSTRZEŻENIE", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jButtonResolveActionPerformed

    /**
     * Wybranie przykładowego zadania dla probelmu kombinatorycznego
     * @param evt 
     */
    private void jComboBoxTaskExampleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxTaskExampleActionPerformed
        // TODO add your handling code here:
        switch(jComboBoxTaskExample.getSelectedIndex())
        {
            case 0:
                this.jTextFieldUserInput.setText("SEND\r\n+MORE\r\n=MONEY");
                break;
            case 1:
                this.jTextFieldUserInput.setText("ONA\r\n+MA\r\n+COS\r\n+DLA\r\n=MNIE");
                break;
            case 2:
                this.jTextFieldUserInput.setText("ZJEM\r\n+DZIS\r\n=OBIAD");
                break;
            case 3:
                this.jTextFieldUserInput.setText("ONA\r\n-MA\r\n-COS\r\n+DLA\r\n=MNIE");
                break;
            default:
                break;
        }
    }//GEN-LAST:event_jComboBoxTaskExampleActionPerformed

    /**
     * Wyczyszczenie kontrolek reprezentujących rozwiązanie
     * dla problemu planowania/harmonogramowania
     * @param evt 
     */
    private void jButtonClearPTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonClearPTActionPerformed
        // TODO add your handling code here:
        ((DefaultTableModel)this.jTableGameScheduleRound1.getModel()).setRowCount(0);
        ((DefaultTableModel)this.jTableGameScheduleRound2.getModel()).setRowCount(0);
        this.jTextAreaUserInf.setText("");
    }//GEN-LAST:event_jButtonClearPTActionPerformed

    /**
     * Rozwiązanie problemu planowania/harmonogramowania
     * @param evt 
     */
    private void jButtonResolvePTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonResolvePTActionPerformed
        // TODO add your handling code here:
        this.jButtonClearPTActionPerformed(null);   // Wyczyszczenie zawartości pól rozwiązań dla probelmu PT

        ArrayList<FootballTeam> teamsArray = DataConverter.tableToTeamArray(mTeamsTable);
        //ArrayList<FootballField> fieldsArray = DataConverter.tableToFieldArray(mFieldsTable);
        ArrayList<Number> timeList = getTimeArray();

        try
        {
            JaCoPEngine appPTEngine = new PlanningTaskEngine(teamsArray, timeList);

            appPTEngine.createModel();

            if(appPTEngine.searchSolution())
            {
                String solution = DataConverter.PlanningTaskGetSolutionDetails(appPTEngine.getSearchMethod());
                this.jTextAreaUserInf.setText("Rozwiązanie zostało znalezione!\n\n"+solution);

                DataConverter.PlanningTaskSolution(appPTEngine.getSearchMethod(), jTableGameScheduleRound1, jTableGameScheduleRound2, teamsArray);
            }
            else
            this.jTextAreaUserInf.setText("Nie udało się znaleźć rozwiązania");
        }
        catch(Exception e)
        {
            String str = "Dane problemu zostały wprowadzone niepoprawnie\nlub nie wprowadzono danych!";
            JOptionPane.showMessageDialog(rootPane, str, "OSTRZEŻENIE", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jButtonResolvePTActionPerformed

    /**
     * Otwarcie okna edycji ram czasowych dla problemu planowania/harmonogramowania
     * @param evt 
     */
    private void jButtonEnterTimeSpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEnterTimeSpanActionPerformed
        // TODO add your handling code here:
        mConfigWindow.setTitle("Ramy czasowe");
        mConfigWindow.setContentPane(mTimePanel);
        mConfigWindow.pack();
        mConfigWindow.setVisible(true);
    }//GEN-LAST:event_jButtonEnterTimeSpanActionPerformed

    /**
     * Otwarcie okna edycji drużyn dla problemu planowania/harmonogramowania
     * @param evt 
     */
    private void jButtonEnterTeamsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEnterTeamsActionPerformed
        // TODO add your handling code here:
        mConfigWindow.setTitle("Drużyny");
        mConfigWindow.setContentPane(mTeamPanel);
        mConfigWindow.pack();
        mConfigWindow.setVisible(true);
    }//GEN-LAST:event_jButtonEnterTeamsActionPerformed

    /**
     * 
     * @param evt 
     */
    private void jBtnConfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnConfigActionPerformed
        mConfigWindow.setTitle("Konfiguracja trasy");
        mConfigWindow.setContentPane(mCostPlanerPanel);
        mConfigWindow.pack();
        mConfigWindow.setVisible(true);
    }//GEN-LAST:event_jBtnConfigActionPerformed

    private static class MapRecalcListener implements MouseListener {

        MapVisualizer mv;
        javax.swing.JLabel label;
        public MapRecalcListener(MapVisualizer v, javax.swing.JLabel targetLabel) {
            mv = v; 
            v.addMouseListener(this);
            label = targetLabel;
        }
        @Override
        public void mouseClicked(MouseEvent e) {
            OptimizationTaskEngine ote = new OptimizationTaskEngine(mv.mapModel());
            ote.createModel();

            if(ote.searchSolution())
            {
                System.out.println("Znaleziono rozwiązanie");
            }
            else
            {
                System.out.println("Nieznaleziono rozwiązania");
            }

            MapTrack mt = ote.getTrackSolution();
            mv.mapModel().setCurrentTrack(mt);
            mv.repaint();
            
            double realCost = mt.calculateCost(MapTrack.CostType.Real);
            double optCost = mt.calculateCost(MapTrack.CostType.Optimized);
            
            String str = String.format("Koszt optymalny: %f, rzeczywisty: %f", realCost, optCost);
            label.setText(str);
        }

        @Override
        public void mousePressed(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }
  
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainWindow().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.Box.Filler filler1;
    private javax.swing.JButton jBtnConfig;
    private javax.swing.JButton jButtonClear;
    private javax.swing.JButton jButtonClearPT;
    private javax.swing.JButton jButtonEnterTeams;
    private javax.swing.JButton jButtonEnterTimeSpan;
    private javax.swing.JButton jButtonResolve;
    private javax.swing.JButton jButtonResolvePT;
    private javax.swing.JComboBox jComboBoxTaskExample;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private poproject.GUI.MapVisualizer jMapVisualizer;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItemClose;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanelCombinatorialTask;
    private javax.swing.JPanel jPanelOptimizationTask;
    private javax.swing.JPanel jPanelPlanningTask;
    private javax.swing.JPanel jPanelRound1;
    private javax.swing.JPanel jPanelRound2;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTabbedPane jTabPanel;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTableGameScheduleRound1;
    private javax.swing.JTable jTableGameScheduleRound2;
    private javax.swing.JTextArea jTextAreaUserInf;
    private javax.swing.JTextPane jTextFieldSolution;
    private javax.swing.JTextPane jTextFieldUserInput;
    private javax.swing.JLabel jTrackResult;
    // End of variables declaration//GEN-END:variables
}
